﻿// ===============================================================================
// Project Name        :    Gfw.Repositories.Account
// Project Description :    
// ===============================================================================
// Class Name          :    ActCompanyRechargeRepository
// Namespace:          :    Gfw.Repositories.Account
// Class Version       :    v1.0.0.0
// Class Description   :    
// CLR Version:        :    4.0.30319.17626
//Author               :    lif
// Machine             :    ADICO-PC
// Create Time         :    2012/6/24 10:19:35
// Update Time         :    2012/6/24 10:19:35
// ===============================================================================
// Copyright © lif 2012 . All rights reserved.
// ===============================================================================
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Gfw.Models.Account;
using Gfw.Repository;

namespace Gfw.Repositories.Account
{
    public class ActCompanyRechargeRepository : Repository<ActCompanyRecharge>
    {
        /// <summary>
        /// 条件查询用户充值记录
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="pageIndex">base on 0</param>
        /// <param name="pageSize"></param>
        /// <param name="pageTotal"></param>
        /// <param name="countRecharge"></param>
        /// <param name="sumRecharge"></param>
        /// <param name="maxRecharge"></param>
        /// <param name="minRecharge"></param>
        /// <returns></returns>
        public IList<ActCompanyRecharge> QueryRecharge(string userID, DateTime start, DateTime end, int pageIndex, int pageSize, 
            out int pageTotal, out int countRecharge ,out decimal sumRecharge, out decimal maxRecharge, out decimal minRecharge)
        {
            pageTotal = countRecharge = 0;
            sumRecharge = maxRecharge = minRecharge = 0.0M;

            try
            {
                end = end.AddDays(1);

                var result = Session.CreateQuery("select count(*) as COUNT, sum(RechargeMoney) as SUM, max(RechargeMoney) as MAX, min(RechargeMoney) as MIN from ActCompanyRecharge where Userid=:userID" +
                " and RechargeDate>=:Start and RechargeDate<=:End").SetString("userID", userID).SetDateTime("Start", start)
                .SetDateTime("End", end).List<object[]>();

                int total = 0;
                if (result != null && result[0] != null)
                {
                    total = countRecharge = int.Parse(result[0][0] != null ? result[0][0].ToString() : "0");
                    sumRecharge = decimal.Parse(result[0][1] != null ? result[0][1].ToString() : "0.0");
                    maxRecharge = decimal.Parse(result[0][2] != null ? result[0][2].ToString() : "0.0");
                    minRecharge = decimal.Parse(result[0][3] != null ? result[0][3].ToString() : "0.0");
                }

                //int total = (int)Session.CreateQuery("select count(*) from ActCompanyRecharge where Userid=:userID" +
                //    " and RechargeDate>=:Start and RechargeDate<=:End").SetString("userID", userID).SetDateTime("Start", start)
                //    .SetDateTime("End", end).UniqueResult<long>();
                pageTotal = (total + pageSize - 1) / pageSize;

                var query = Session.CreateQuery("from ActCompanyRecharge where Userid=:userID" +
                    " and RechargeDate>=:Start and RechargeDate<=:End order by RechargeDate desc").SetString("userID", userID).SetDateTime("Start", start)
                    .SetDateTime("End", end).SetFirstResult(pageIndex * pageSize).SetMaxResults(pageSize);

                return query.List<ActCompanyRecharge>();
            }
            catch (System.Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return null;
            }
        }
    }
}
